Java List的remove()方法踩坑

您所在的位置:网站首页 想见你 吉他 Java List的remove()方法踩坑

Java List的remove()方法踩坑

#Java List的remove()方法踩坑| 来源: 网络整理| 查看: 265

目录1、普通for循环遍历List删除指定元素--错误!!!2、for循环遍历List删除元素时,让索引同步调整--正确!3、倒序遍历List删除元素--正确!4、foreach遍历List删除元素--错误!!!5、迭代删除List元素--正确!6、迭代遍历,用list.remove(i)方法删除元素--错误!!!7、List删除元素时,注意Integer类型和int类型的区别.总结:

Java的List在删除元素时,一般会用list.remove(o)/remove(i)方法。在使用时,容易触碰陷阱,得到意想不到的结果。总结以往经验,记录下来与大家分享。

首先初始化List,代码如下:

package com.cicc.am.test; import java.util.ArrayList; import java.util.List; public class ListTest { public static void main(String[] args) { List list=new ArrayList(); list.add(1); list.add(2); list.add(3); list.add(3); list.add(4); System.out.println(list); } }

输出结果为[1, 2, 3, 3, 4]

1、普通for循环遍历List删除指定元素--错误!!! for(int i=0;i= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }

通过代码我们发现 Itr 是 ArrayList 中定义的一个私有内部类,在 next、remove方法中都会调用checkForComodification 方法,该方法的 作用是判断 modCount != expectedModCount是否相等,如果不相等则抛出ConcurrentModificationException异常。每次正常执行 remove 方法后,都会对执行expectedModCount = modCount赋值,保证两个值相等,那么问题基本上已经清晰了,在 foreach 循环中

执行 list.remove(item);,对 list 对象的 modCount 值进行了修改,而 list 对象的迭代器的 expectedModCount 值未进行修改,因此抛出了ConcurrentModificationException异常。

5、迭代删除List元素--正确!

java中所有的集合对象类型都实现了Iterator接口,遍历时都可以进行迭代:

Iterator it=list.iterator(); while(it.hasNext()){ if(it.next()==3){ it.remove(); } } System.out.println(list);

输出结果:[1, 2, 4]

Iterator.remove() 方法会在删除当前迭代对象的同时,会保留原来元素的索引。所以用迭代删除元素是最保险的方法,建议大家使用List过程

中需要删除元素时,使用这种方式。

6、迭代遍历,用list.remove(i)方法删除元素--错误!!! Iterator it=list.iterator(); while(it.hasNext()){ Integer value=it.next(); if(value==3){ list.remove(value); } } System.out.println(list);

抛出异常:java.util.ConcurrentModificationException,原理同上述方法4.

7、List删除元素时,注意Integer类型和int类型的区别.

上述Integer的list,直接删除元素2,代码如下:

list.remove(2); System.out.println(list);

输出结果:[1, 2, 3, 4]

可以看出,List删除元素时传入数字时,默认按索引删除。如果需要删除Integer对象,调用remove(object)方法,需要传入Integer类型,代码如下:

list.remove(new Integer(2)); System.out.println(list);

输出结果:[1, 3, 3, 4]

总结:

   1、用for循环遍历List删除元素时,需要注意索引会左移的问题。

   2、List删除元素时,为避免陷阱,建议使用迭代器iterator的remove方式。

   3、List删除元素时,默认按索引删除,而不是对象删除。

到此这篇关于Java List的remove()方法踩坑的文章就介绍到这了,更多相关Java List remove()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:Java常见踩坑记录之异常处理java使用BeanUtils.copyProperties踩坑经历java8 stream的多字段排序实现(踩坑)Java踩坑记录之BigDecimal类Java踩坑记录之Arrays.AsList一不小心就让Java开发踩坑的fail-fast是个什么鬼?(推荐)Java中数组协变和范型不变性踩坑记录java学习之JasperReport踩坑你还不懂栈和队列的实现吗


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3